package cn.iocoder.yudao.module.testequipmentuse.controller.admin.testequipmentuse;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.testequipmentuse.controller.admin.testequipmentuse.vo.*;
import cn.iocoder.yudao.module.testequipmentuse.dal.dataobject.testequipmentuse.TestEquipmentUseDO;
import cn.iocoder.yudao.module.testequipmentuse.service.testequipmentuse.TestEquipmentUseService;

@Tag(name = "管理后台 - 参试设备使用记录表")
@RestController
@RequestMapping("/testequipmentuse/test-equipment-use")
@Validated
public class TestEquipmentUseController {

    @Resource
    private TestEquipmentUseService testEquipmentUseService;

    @PostMapping("/create")
    @Operation(summary = "创建参试设备使用记录表")
    @PreAuthorize("@ss.hasPermission('testequipmentuse:test-equipment-use:create')")
    public CommonResult<Long> createTestEquipmentUse(@Valid @RequestBody TestEquipmentUseSaveReqVO createReqVO) {
        return success(testEquipmentUseService.createTestEquipmentUse(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新参试设备使用记录表")
    @PreAuthorize("@ss.hasPermission('testequipmentuse:test-equipment-use:update')")
    public CommonResult<Boolean> updateTestEquipmentUse(@Valid @RequestBody TestEquipmentUseSaveReqVO updateReqVO) {
        testEquipmentUseService.updateTestEquipmentUse(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除参试设备使用记录表")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('testequipmentuse:test-equipment-use:delete')")
    public CommonResult<Boolean> deleteTestEquipmentUse(@RequestParam("id") Long id) {
        testEquipmentUseService.deleteTestEquipmentUse(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除参试设备使用记录表")
                @PreAuthorize("@ss.hasPermission('testequipmentuse:test-equipment-use:delete')")
    public CommonResult<Boolean> deleteTestEquipmentUseList(@RequestParam("ids") List<Long> ids) {
        testEquipmentUseService.deleteTestEquipmentUseListByIds(ids);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得参试设备使用记录表")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('testequipmentuse:test-equipment-use:query')")
    public CommonResult<TestEquipmentUseRespVO> getTestEquipmentUse(@RequestParam("id") Long id) {
        TestEquipmentUseDO testEquipmentUse = testEquipmentUseService.getTestEquipmentUse(id);
        return success(BeanUtils.toBean(testEquipmentUse, TestEquipmentUseRespVO.class));
    }


    @GetMapping("/page")
    @Operation(summary = "获得参试设备使用记录分页")
    @PreAuthorize("@ss.hasPermission('test-equipment-use:query')")
    public CommonResult<PageResult<TestEquipmentUseRespVO>> getTestEquipmentUsePage(@Valid TestEquipmentUsePageReqVO pageReqVO) {
        // 核心修改：接收类型改为 Service 实际返回的 PageResult<TestEquipmentUseRespVO>
        PageResult<TestEquipmentUseRespVO> pageResult = testEquipmentUseService.getTestEquipmentUsePageWithEquipmentInfo(pageReqVO);
        // 无需再手动转换（Service 内部已完成 DO → VO 转换），直接返回
        return success(pageResult);
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出参试设备使用记录表 Excel")
    @PreAuthorize("@ss.hasPermission('testequipmentuse:test-equipment-use:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportTestEquipmentUseExcel(@Valid TestEquipmentUsePageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<TestEquipmentUseDO> list = testEquipmentUseService.getTestEquipmentUsePage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "参试设备使用记录表.xls", "数据", TestEquipmentUseRespVO.class,
                        BeanUtils.toBean(list, TestEquipmentUseRespVO.class));
    }

    @GetMapping("/page-with-equipment-out")
    @Operation(summary = "获得已出库的参试设备使用记录分页（包含设备信息）")
    @PreAuthorize("@ss.hasPermission('testequipmentuse:test-equipment-use:query')")
    public CommonResult<PageResult<TestEquipmentUseWithEquipmentRespVO>> getTestEquipmentUseOutPageWithEquipmentInfo(
            @Valid TestEquipmentUsePageReqVO pageReqVO) {
        PageResult<TestEquipmentUseWithEquipmentRespVO> pageResult = testEquipmentUseService.
                getTestEquipmentUseOutPageWithEquipmentInfo(pageReqVO);
        return success(pageResult);
    }
    
    @GetMapping("/my-equipment-out")
    @Operation(summary = "获得我的已出库设备使用记录")
    @PreAuthorize("@ss.hasPermission('testequipmentuse:test-equipment-use:query')")
    public CommonResult<PageResult<TestEquipmentUseWithEquipmentRespVO>> getMyEquipmentUseOutPage(
            @Valid TestEquipmentUsePageReqVO pageReqVO,
            @RequestParam(defaultValue = "1") Integer pageNo,
            @RequestParam(defaultValue = "10") Integer pageSize) {
        pageReqVO.setPageNo(pageNo);
        pageReqVO.setPageSize(pageSize);
        PageResult<TestEquipmentUseWithEquipmentRespVO> pageResult = testEquipmentUseService.
                getMyEquipmentUseOutPage(pageReqVO);
        return success(pageResult);
    }


}